package com.zj.aspects;

import com.zj.service.GrabResult;
import com.zj.web.bean.MyUserDTO;
import com.zj.web.bean.UserRedPacketDTO;
import lombok.extern.java.Log;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Log
@Order(12)
public class RedPacketAspect {
    @Autowired
    private JmsRedPacketMessageProducter jmsMessageProductr;
    @Pointcut("execution(* com.zj.service.RedPacketServiceImpl.grabRedPacket(..))")
    public void RedPacketPointcut() {
    }
    //通知的类型 before after afterReturning afterThrowing around
    @AfterReturning(value = "RedPacketPointcut()",returning = "obj")
    public void sendEmailMessageToMQ(JoinPoint jp, Object obj){ //jp是连接点，即原登录方法 ，通过Jp可以获取到登录方法的参数，返回值，异常信息
        log.info("抢红包成功后，发送一条要发送给MQ的消息");
        if (obj==null && !(obj instanceof UserRedPacketDTO)){
            return;
        }
        GrabResult grabResult = (GrabResult) obj;
        if (!grabResult.isSuccess()){
            return;
        }
        Object[] args = jp.getArgs();
        UserRedPacketDTO userRedPacketDTO=new UserRedPacketDTO();
        userRedPacketDTO.setActivityId((Long) args[0]);
        userRedPacketDTO.setUserId((Long) args[1]);
        userRedPacketDTO.setAmount(grabResult.getAmount());
        userRedPacketDTO.setReceivedAt(grabResult.getReceivedAt());
        log.info("抢红包成功的用户信息："+userRedPacketDTO.toString());
        jmsMessageProductr.sendMessage(userRedPacketDTO);
    }
}
